package kr.saeildamil.androidstudy.ch19;

import kr.saeildamil.androidstudy.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class HandlerTestt extends Activity {
	int mMainValue;
	int mBackValue;
	TextView MainValueTv;
	TextView BackValueTv;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch19_threadtestt);

		MainValueTv = (TextView) findViewById(R.id.ch19_threadtest_mainvalue);
		BackValueTv = (TextView) findViewById(R.id.ch19_threadtest_backvalue);
		
		BackThread thread = new BackThread();
		thread.setDaemon(true);
		thread.start();
	}// end of method onCreate();

	public void mOnClick(View v) {
		mMainValue++;
		MainValueTv.setText("MAIN VALUE : " + mMainValue);
	}// end of method mOnClick()

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 0) {
				BackValueTv.setText("BACK VALUE : " + mBackValue);
			}
		}//end of method handleMessage
	};
	
	class BackThread extends Thread {
		public void run(){
			super.run();
			while(true){
				mBackValue++;
				mHandler.sendEmptyMessage(0);
				try {Thread.sleep(1000);} 
				catch (InterruptedException e) {e.printStackTrace();}
			}
		}//end of method run()
	}//end of class BackThread
}
